package info.pello.games.libgdx.pong;


import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;

public class PongGame implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	private Texture texturaPala1;
	private TextureRegion regionPala1;
	private Texture texturaPala2;
	private TextureRegion regionPala2;
	private Texture texturaPelota;
	private TextureRegion regionPelota;
	private World world;
	Vector3 puntoDeToque;
	private BitmapFont textForGame;
	private BitmapFont scoreFontPlayer1;
	private BitmapFont scoreFontPlayer2;
	
	
	@Override
	public void create() {		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		puntoDeToque = new Vector3();
		
		world = new World();
		camera = new OrthographicCamera(10, 15);
		camera.position.set(10f / 2, 15f / 2, 0);
		
		batch = new SpriteBatch();

		// Cargamos la textura de la pala1
		texturaPala1 = new Texture(Gdx.files.internal("data/pala.jpg"));
		texturaPala1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		regionPala1 = new TextureRegion(texturaPala1, 0,0,64,8);
		
		//cargamos la textura de la pala2
		texturaPala2 = new Texture(Gdx.files.internal("data/pala.jpg"));
		texturaPala2.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		//A partir de la coordenada (0,0) coje 64x8 pixeles
		regionPala2 = new TextureRegion(texturaPala2, 0,0,64,8);
		
		// Cargamos la textura de la pelota
		texturaPelota = new Texture(Gdx.files.internal("data/pelota.jpg"));
		
		texturaPelota.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		regionPelota = new TextureRegion(texturaPelota, 0,0,16,16);
		
		
		//cargamos el archivo de la fuente en el BitmapFont
		 scoreFontPlayer1 = new BitmapFont(Gdx.files.internal("data/emulogic.fnt"), Gdx.files.internal("data/emulogic.png"), false);
		 scoreFontPlayer2 = new BitmapFont(Gdx.files.internal("data/emulogic.fnt"), Gdx.files.internal("data/emulogic.png"), false);
		camera.update();
		batch.setProjectionMatrix(camera.combined);
	}

	@Override
	public void dispose() {
		batch.dispose();
		texturaPala1.dispose();
		texturaPala2.dispose();
		texturaPelota.dispose();
	}

	@Override
	public void render() {		
		
		
		textForGame = new BitmapFont();
		// le ponemos un color
		textForGame.setColor(Color.WHITE);
		// y le aumentamos el tamaño
		textForGame.setScale(4f);
		
		if (Gdx.input.isTouched()){
			camera.unproject(puntoDeToque.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			if(Gdx.input.getY() < 240){
				world.getPala2().mover(puntoDeToque.x - 1);				
			}else{
				world.getPala1().mover(puntoDeToque.x - 1);
			}
			
		}
		
		   
		 world.update();
		 
		// Borramos la pantalla ponemos todo a NEGRO
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
		batch.setProjectionMatrix(camera.combined);
		
		batch.enableBlending();
		
		batch.begin();
		scoreFontPlayer1.setScale(0.04f);
		scoreFontPlayer2.setScale(0.04f);
		//sprite.draw(batch);
		//batch.draw(Assets.background, 0, 0, 15, 10);
		//batch.draw(Assets.lft, 0, 0, 1, 1);
		//batch.draw(Assets.right, 14, 0, 1, 1);
		//batch.draw(texturaPala, 2,3);
		//batch.draw(texturaPelota, 0,0);
		batch.draw(regionPala2, world.getPala2().posicion.x,world.getPala2().posicion.y,3,0.5f);    
		batch.draw(regionPelota, world.getPelota().posicion.x,world.getPelota().posicion.y,0.5f,0.5f);
		batch.draw(regionPala1, world.getPala1().posicion.x,world.getPala1().posicion.y,3,0.5f);
		scoreFontPlayer1.draw(batch, "Player1 : " + world.getPala1().puntos, 1, 12); 
		scoreFontPlayer2.draw(batch, "Player2 : " + world.getPala2().puntos, 1, 2);
		
		batch.end();

	}

	@Override
	public void resize(int width, int heigcht) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
